<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Setting extends Controller
{
    //
    public function index()
    {
        $data['setting'] = DB::table('setting')->where('names','site_setting')->item();
        if($data['setting']){
            $data['setting']['vals'] = json_decode($data['setting']['vals'],true);
        }
        $data['logo'] = DB::table('setting')->where('names','site_logo')->item();
        if($data['logo']){
            $data['logo']['vals'] = json_decode($data['logo']['vals'],true);
        }
        return view('admin.setting.index',$data);
    }
    // 保存
    public function save(Request $request){
        $data['title'] = $request->title;
        $data['keywords'] = $request->keywords;
        $data['descs'] = $request->descs;
        $data['icp'] = $request->icp;
        $data['rcode'] = $request->rcode;
        $thumb    = trim($request->thumb);

        if(!$data['title']){
            exit(json_encode(array('code'=>1,'msg'=>'标题不能为空')));
        }
        $item = DB::table('setting')->where('names','site_setting')->item();
        if($item){

            DB::table('setting')->where('names','site_setting')->update(array('names'=>'site_setting','vals'=>json_encode($data)));
        }else{

            DB::table('setting')->insert(array('names'=>'site_setting','vals'=>json_encode($data)));
        }
        $res = DB::table('setting')->where('names','site_logo')->item();
        if ($res){
            DB::table('setting')->where('names','site_logo')->update(array('names'=>'site_logo','vals'=>json_encode($thumb)));
        }else{
            DB::table('setting')->insert(array('names'=>'site_logo','vals'=>json_encode($thumb)));
        }

        exit(json_encode(array('code'=>0,'msg'=>'保存成功')));
    }
    public function short()
    {
        $data['setting'] = DB::table('setting')->where('names','site_short')->item();
        if($data['setting']){
            $data['setting']['vals'] = json_decode($data['setting']['vals'],true);
        }
        return view('admin.setting.short',$data);
    }
    public function short_save(Request $request)
    {
        $data['key'] = $request->key;
        $data['secret'] = $request->secret;
        $data['signname'] = $request->signname;
        $data['code'] = $request->code;

        if(!$data['key']){
            exit(json_encode(array('code'=>1,'msg'=>'AccessKeyID不能为空')));
        }
        if(!$data['secret']){
            exit(json_encode(array('code'=>1,'msg'=>'AccessKey Secret不能为空')));
        }
        if(!$data['signname']){
            exit(json_encode(array('code'=>1,'msg'=>'签名不能为空')));
        }
        if(!$data['code']){
            exit(json_encode(array('code'=>1,'msg'=>'模板ID不能为空')));
        }
        $item = DB::table('setting')->where('names','site_short')->item();
        if($item){
            DB::table('setting')->where('names','site_short')->update(array('names'=>'site_short','vals'=>json_encode($data)));
        }else{
            DB::table('setting')->insert(array('names'=>'site_short','vals'=>json_encode($data)));
        }

        exit(json_encode(array('code'=>0,'msg'=>'保存成功')));
    }
}
